package com.xunge.controller;

import com.xunge.anno.Log;
import com.xunge.pojo.ExamRecord;
import com.xunge.pojo.ExamRecordQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.pojo.Result;
import com.xunge.service.ExamRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/exam-record")
@RestController
public class ExamRecordController {
    @Autowired
    private ExamRecordService examRecordService;

    /**
     * 分页查询考试记录信息
     */
    @GetMapping
    public Result page(ExamRecordQueryParam examRecordQueryParam){
        log.info("分页请求参数:{}" ,examRecordQueryParam);
        PageResult<ExamRecord> pageResult = examRecordService.page(examRecordQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 保存考试记录
     */
    @Log
    @PostMapping
    public Result save(@RequestBody ExamRecord examRecord){
        log.info("保存信息：{}",examRecord);
        examRecordService.save(examRecord);
        return Result.success();
    }

    /**
     * 批量删除考试记录
     */
    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("根据ID批量删除考试记录: {}",ids);
        examRecordService.delete(ids);
        return Result.success();
    }

    /**
     * 根据ID查询考试记录
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据ID查询考试记录: {}",id);
        ExamRecord examRecord = examRecordService.getById(id);
        return Result.success(examRecord);
    }

    /**
     * 根据ID修改考试记录
     */
    @Log
    @PutMapping
    public Result update(@RequestBody ExamRecord examRecord){
        log.info("根据ID修改考试记录：{}",examRecord);
        examRecordService.update(examRecord);
        return Result.success();
    }

    /**
     * 根据考试ID查询记录
     */
    @GetMapping("/exam/{examId}")
    public Result getByExamId(@PathVariable Integer examId){
        log.info("根据考试ID查询记录: {}",examId);
        List<ExamRecord> records = examRecordService.getByExamId(examId);
        return Result.success(records);
    }

    /**
     * 根据学生ID查询记录
     */
    @GetMapping("/student/{studentId}")
    public Result getByStudentId(@PathVariable Integer studentId){
        log.info("根据学生ID查询记录: {}",studentId);
        List<ExamRecord> records = examRecordService.getByStudentId(studentId);
        return Result.success(records);
    }

    /**
     * 根据考试ID和学生ID查询记录
     */
    @GetMapping("/exam/{examId}/student/{studentId}")
    public Result getByExamIdAndStudentId(@PathVariable Integer examId, @PathVariable Integer studentId){
        log.info("根据考试ID和学生ID查询记录: examId={}, studentId={}", examId, studentId);
        ExamRecord record = examRecordService.getByExamIdAndStudentId(examId, studentId);
        return Result.success(record);
    }
} 